package com.dearbinge.data.service.impl.withdrawDeposit;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.withdrawDeposit.WithdrawDepositService;
import com.dearbinge.data.dao.UserWalletMapper;
import com.dearbinge.data.dao.WithdrawDepositMapper;
import com.dearbinge.data.pojo.withdrawDeposit.WithdrawDeposit;
import com.dearbinge.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service("withdrawDepositService")
public class WithdrawDepositServiceImpl implements WithdrawDepositService {

    @Autowired
    private WithdrawDepositMapper withdrawDepositMapper;

    @Autowired
    private UserWalletMapper userWalletMapper;

    @Override
    public Integer insert(WithdrawDeposit withdrawDeposit) {
        Integer dataCode = 0;
        Integer money = userWalletMapper.updateMoney(withdrawDeposit.getUserId(),withdrawDeposit.getMoney());
        if (money > 0) {
            dataCode = withdrawDepositMapper.insert(withdrawDeposit);
        }

        return dataCode;
    }

    @Override
    public Integer update(WithdrawDeposit withdrawDeposit) {
        Integer dataCode = withdrawDepositMapper.update(withdrawDeposit);
        return dataCode;
    }

    @Override
    public Integer delete(Integer id) {
        Integer dataCode = withdrawDepositMapper.delete(id);
        return dataCode;
    }

    @Override
    public Page getByPage(String userId, Integer pageNo, Integer pageSize) {
        pageNo = PageUtils.getDefaultValues(pageNo, 1);
        pageSize = PageUtils.getDefaultValues(pageSize, 10);
        Page<WithdrawDeposit> page = new Page<>(pageNo,pageSize);
        Integer count = withdrawDepositMapper.count(userId);
        page.setTotal(count);
        List<WithdrawDeposit> list = withdrawDepositMapper.getByPage(userId,page);
        page.setRecords(list);
        return page;
    }
}
